package com.syn.synold4s.vm;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.gaoyi.basemodule.apiresult.ApiResultData;
import com.gaoyi.basemodule.baseCallback.OnResultListener;
import com.gaoyi.basemodule.baseUtil.OKHttpUtil;
import com.gaoyi.basemodule.baseUtil.SPUtil;
import com.gaoyi.basemodule.baseUtil.ThreadManager;
import com.gaoyi.basemodule.baseUtil.ToastUtil;
import com.gaoyi.basemodule.basebean.BaseQuery;
import com.gaoyi.basemodule.basebean.LoginDto;
import com.gaoyi.basemodule.basebean.UserTagDto;
import com.gaoyi.basemodule.baseconstant.ApiStatus;
import com.gaoyi.basemodule.baseconstant.SPName;
import com.gaoyi.basemodule.basevm.BaseViewModel;
import com.google.gson.reflect.TypeToken;
import com.syn.synold4s.R;
import com.syn.synold4s.bean.dto.CctvDto;
import com.syn.synold4s.repository.MainRepository;
import com.syn.synold4s.repository.SurveillanceRepository;

import java.util.List;

public class SurveillanceViewModel extends BaseViewModel {

    private MutableLiveData<List<CctvDto.CCTVModel>> cctv;
    private SurveillanceRepository surveillanceRepository;
    final MainRepository mRepository;
    MutableLiveData<LoginDto> mTagByProj;
    MutableLiveData<List<UserTagDto>> mUserTag;

    public SurveillanceViewModel(@NonNull Application application) {
        super(application);
        mRepository = new MainRepository();
        surveillanceRepository = new SurveillanceRepository();
    }

    public MutableLiveData<List<CctvDto.CCTVModel>> getCctvResult() {
        if (cctv == null) {
            cctv = new MutableLiveData<>();
        }
        return cctv;
    }

    public MutableLiveData<List<UserTagDto>> getUserTag() {
        if (mUserTag == null) {
            mUserTag = new MutableLiveData<>();
        }
        return mUserTag;
    }

    public MutableLiveData<LoginDto> getTagByProj() {
        if (mTagByProj == null) {
            mTagByProj = new MutableLiveData<>();
        }
        return mTagByProj;
    }

    public void getCctv(int pageIndex, String keyWord) {
        BaseQuery query = new BaseQuery();
        query.setPageIndex(pageIndex);
        query.setPage(true);
        query.setPageSize(6);
        query.setKeyWord(keyWord);
        if (getView() != null) {
            getView().showLoading();
        }
        surveillanceRepository.cctv(query, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ApiResultData<CctvDto> apiResultData = (ApiResultData<CctvDto>) apiResult;
                mResultCode.setValue(apiResultData.getStatus());
                if (apiResultData.getStatus() != ApiStatus.SUCCESS) {
                    ToastUtil.showToast(apiResultData.getMessage());
                }
                CctvDto data = apiResultData.getData();
                if (data == null) {
                    return;
                }
                if (data.getCctv() != null) {
                    Log.e(this.getClass().getSimpleName(), "onSuccess: " + data.getCctv().size());
                    cctv.setValue(data.getCctv());
                }

            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                mResultCode.setValue(ApiStatus.NET_ERROR);
                ToastUtil.showToast(R.string.network_error);
            }
        }, new TypeToken<ApiResultData<CctvDto>>() {
        }.getType());
    }

    public void getTag() {
        ThreadManager.getInstance().execute(() -> {
            LoginDto loginDto = mRepository.getTagByProjId(SPUtil.getInstance().getString(SPName.PROJ_ID));
            if (loginDto != null) {
                OKHttpUtil.getInstance().post(() -> mTagByProj.setValue(loginDto));
            }
        });
    }
}
